//
//  OosCheckinParser.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/29/11.
//

#import "OosCheckinFeedParser.h"
#import <oos4ios/Model/OosService.h>
#import <oos4ios/Model/OosCheckin.h>
#import <oos4ios/Model/OosUser.h>
#import <oos4ios/Model/OosAvatarImage.h>

#import "../Atom/Feeds/OosCheckinFeed.h"
#import "../Atom/Entries/OosCheckinEntry.h"
#import "OosServiceEntryParser.h"
#import "OosCheckinEntryParser.h"


@implementation OosCheckinFeedParser

static OosCheckinFeedParser *instance;

+ (OosCheckinFeedParser *) instance
{
	if (!instance) {
		instance = [[OosCheckinFeedParser alloc] init];
	}
	return instance;
}

- (NSArray *) parseFeed:(OosFeed *)feed
{
	OosCheckinFeed *checkinFeed = (OosCheckinFeed *) feed;
	
	NSMutableArray *results = [NSMutableArray array];
    OosCheckinEntryParser *parser = [OosCheckinEntryParser instance];
	for (OosCheckinEntry* entry in [checkinFeed entries]) {
        if (entry.slug != nil) {
            [results addObject:[parser parseEntry:entry]];
        } else {
            // TODO handle facebook checkins
        }
	}
	return results;
}

#pragma mark - json addons

- (NSArray *)parseJsonFeed:(NSString *)jsonFeed
{
    NSDictionary *rawFeed = (NSDictionary *) [jsonFeed JSONValue];
    
    NSMutableArray *results = [NSMutableArray array];
    
    NSArray *entries = [rawFeed objectForKey:@"entries"];
    OosCheckinEntryParser *parser = [OosCheckinEntryParser instance];
    for (NSDictionary *entry in entries) {
        if ([entry objectForKey:@"oos_slug"] != nil) {
            [results addObject:[parser parseJsonEntry:entry]];
        }
    }
    
    return results;
}

@end
